package MapReduce.writable;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class FlowMapper extends Mapper<LongWritable, Text,Text, FlowBean> {
    private final Text outK = new Text();
    private final FlowBean outV = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, FlowBean>.Context context) throws IOException, InterruptedException {

        //1.获取一行数据准备处理
        //例如：21 	13568436656	192.168.100.18	www.alibaba.com	2481	24681	200
        String line = value.toString();

        //2.切割数据
        String[] split = line.split("\t");

        //3.抓取想要的数据
        //手机号 上传流量 下载流量 总流量
        String phone = split[1];

        //数据格式原因 从后往前取
        String upFlow = split[split.length - 3];
        String downFlow = split[split.length - 2];
//        String sumFlow = split[split.length - 1];

        //进行封装
        outK.set(phone);
        outV.setUpFlow(Long.parseLong(upFlow));
        outV.setDownFlow(Long.parseLong(downFlow));
        outV.setSumFlow();

        //输出
        context.write(outK,outV);
    }
}
